package com.swh.filter;


import com.swh.entity.User;
import com.swh.servic.UserServic;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//@WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {

    private UserServic userService=new UserServic();



    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        request.setCharacterEncoding("UTF-8");

        // http://localhost:8080/user/checkEmail ---> /user/checkEmail      --> http://localhost:8080/js/charts-custom.js
        // http://localhost:8080/index.jsp ---> /index.jsp                  --> /js/charts-custom.js
        String uri = request.getRequestURI();

        // 访问的目标资源      index.jsp
        String targetName = uri.substring(uri.lastIndexOf("/") + 1);


        if (
                uri.startsWith("/js") ||
                        uri.startsWith("/bootstrap") ||
                        uri.startsWith("/css") ||
                        uri.startsWith("/fonts") ||
                        uri.startsWith("/img") ||
                        uri.startsWith("/upload") ||
                        uri.startsWith("/vendor") ||

                        targetName.equals("index.jsp") ||
                        targetName.equals("checkCode.jsp") ||
                        targetName.equals("forget.jsp") ||
                        targetName.equals("register.jsp") ||
                        targetName.equals("taglib.jsp") ||
                        targetName.equals("checkEmail") ||
                        targetName.equals("wx_login") ||
                        targetName.equals("QQ_login") ||
                        targetName.equals("checkUsername") ||
                        targetName.equals("login") ||
                        targetName.equals("register") ||
                        targetName.equals("logout")

        ) {
            // 统统放行
            filterChain.doFilter(request, response);

            return;
        }

        // 说明是受限资源
        HttpSession session = request.getSession();

        if (session.getAttribute("login") != null) {
            filterChain.doFilter(request, response);

            return;
        }


        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (Cookie temp : cookies) {
                if (temp.getName().equals("login")) {
                    cookie = temp;
                    break;
                }
            }
        }

        if(cookie!=null){
            Long id = Long.parseLong(cookie.getValue());
            User user = userService.findById(id);

            // 手动放入session
            session.setAttribute("login",user);

            // 放行资源
            filterChain.doFilter(request,response);
            return;
        }

        response.sendRedirect("/index.jsp");

    }

    @Override
    public void destroy() {

    }
}
